home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18512 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  46 lines

  1. Path: inforamp.net!usenet
  2. From: rmorin@inforamp.net (Randy Charles Morin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: String operator+ and memory leakage.
  5. Date: Sun, 21 Apr 1996 07:50:13 GMT
  6. Organization: InfoRamp Inc., Toronto, Ontario (416) 363-9100
  7. Message-ID: <4lcerb$4to@sam.inforamp.net>
  8. References: <4l5fok$feo@utopia.hacktic.nl>
  9. NNTP-Posting-Host: ts26-10.tor.istar.ca
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Mike Tavares <MIKET@cdynamics.com> wrote:
  13. >The problem is, when new_string is created and returned through a 
  14. >reference, no one ever deletes it.
  15. >If I change new_string to be an automatic variable I get an error from 
  16. >the compiler about trying to pass a reference to a item that is out of 
  17. >scope.
  18.  
  19. Is this implementation acceptable...
  20.  
  21. String
  22. String::operator+( String& a_string )
  23.     {
  24.     int total_size = string_length + strlen( a_string ) + 1;
  25.     char* temp_array = new char[total_size];
  26.     char* char_array = a_string;
  27.  
  28.     strcpy( temp_array, character_array );
  29.     strcat( temp_array, char_array );
  30.  
  31.     String new_string() = String(temp_array);
  32.     delete [] temp_array;
  33.     return new_string;
  34.     }
  35.  
  36.  
  37. Agrivar
  38.  
  39. aka Randy Charles Morin,
  40. MiddleWorld SoftWare,
  41. Satisfying Your Bit and Bytes,
  42. Canada 1-800-363-3780 
  43. Other  1-905-279-2087
  44. eMail  rmorin@inforamp.net
  45.  
  46.